﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ObjectEditor;
using EQEmu.Database;

namespace EQEmu.DatabasePlugin
{
    public class DatabaseLogUpdateEventArgs : EventArgs
    {
        public DatabaseLogUpdateEventArgs(string text)
        {
            Text = text;
        }

        public string Text
        {
            get;
            private set;
        }
    }

    public class DatabaseLogControlViewModel : ViewModel
    {
        public event EventHandler<DatabaseLogUpdateEventArgs> Update;

        public DatabaseLogControlViewModel(EQEmuContextFactory contextFactory)
        {
            var tracer = contextFactory as TracingEQEmuContextFactory;
            if (tracer != null)
            {
                tracer.ContextCommandExecuting += tracer_ContextCommandExecuting;
                tracer.ContextCommandFailed += tracer_ContextCommandFailed;
                tracer.ContextCommandFinished += tracer_ContextCommandFinished;
            }
        }

        protected void OnUpdate(string text)
        {
            var e = Update;
            if (e != null)
            {
                e(this, new DatabaseLogUpdateEventArgs(text));
            }
        }

        void tracer_ContextCommandFinished(object sender, EFTracingProvider.CommandExecutionEventArgs e)
        {
            OnUpdate("DBCommandFinished :"+ Environment.NewLine +TracingEQEmuContextExtensions.FormattedCommand(e));
        }

        void tracer_ContextCommandFailed(object sender, EFTracingProvider.CommandExecutionEventArgs e)
        {
            OnUpdate("DBCommandFailed   :" + Environment.NewLine + TracingEQEmuContextExtensions.FormattedCommand(e));
        }

        void tracer_ContextCommandExecuting(object sender, EFTracingProvider.CommandExecutionEventArgs e)
        {
            OnUpdate("DBCommandExecuting:" + Environment.NewLine + TracingEQEmuContextExtensions.FormattedCommand(e));
        }
    }
}
